<?
//echo "adasdada"; exit;
//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","kdcust");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_coapicker.php";
$FileTemplate  = "ais_transaction_coapicker_list.html";
$PageSize      = 10 ;
$TableName     = "tblcoa";
$HTMLGridList  = "CustomerGrid";

//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }

//Initiate Database Table For Search;
$FCode = GetParam("skada","");
$FNama = GetParam("description","");

//added by Krisna 27 October 2011
$prefixFCode = GetParam("awalskada","");
//

$Field = GetParam("Field","");
$FormName = GetParam("FormName","");
$BMode = GetParam("BMode","");
$Period = GetParam("period","");
$IndexField = GetParam("idx","");
$QueryGet  = GetQueryString("All", array('PageNum'));
list($PDay,$PMonth,$PYear)=explode("/",GetParam("period",""));
//Check COA

if (GetParam("period","")) {
  $SQL = "SELECT ledger FROM tblcoa WHERE ledger='$PYear' LIMIT 1 ";
  $ledger = $DBConnection->dbc->get_var($SQL);
  if (!strlen($ledger)) $PYear=$PYear-1;
} else {
  $PYear = date("Y",time());
}

//******* DATABASE FORM QUERY ***********
/*
$SQL = "SELECT * FROM `$TableName` ";
$SQLCount = "SELECT count(*) FROM `$TableName` ";
*/
//$mm = GetParam("BMode","");
//echo "$mm"; exit;
if (GetParam("BMode","")) {
	//echo "test"; exit;
    $SQL = GetCOAParam(GetParam("BMode",""),0);
    $SQLCount = GetCOAParam(GetParam("BMode",""),1);
}

if ($FNama)
    $WHERE .= " AND "." namacc like '%".$FNama."%'";
if ($FCode)
    $WHERE .= " AND "." skada like '%".$FCode."%'";
if ($prefixFCode)
	$WHERE .= " AND "." skada like '".$prefixFCode."%'";
if (GetParam("period",""))
    $WHERE .= " AND "." ledger = '$PYear'";

/* modified by dyah <dyah.mahrani@gmail.com>
 * 23 juli 2009
 * added: COA filter based on group <payable/receivable>
 * */
//--------------------------------------------------------

//get parameter whether the group is payable, receivable or none.

$group = GetParam("group","");
if($group=="AP"){

        //get field apaging from tblprofile
        $queryApaging = "select apaging from tblprofile";
        $result  = $DBConnection->dbc->get_results($queryApaging,ARRAY_A);
        $apaging = trim($result[0]["apaging"]);
        $apagingArray = explode(" ", $apaging);
        $apagingArraysize = count($apagingArray);

        //construct 'where clause' for filter AP
        $WHERE .= " AND (";

        for($i = 0; $i < $apagingArraysize; $i++){

            $WHERE .= " skada LIKE '".$apagingArray[$i]."%'";
            if($i == ($apagingArraysize - 1)){
                $WHERE .= ")";
                break;
            }
            $WHERE .= " OR";

        }

}else if($group=="AR"){

        //get field araging from tblprofile
        $queryAraging = "select araging from tblprofile";
        $result  = $DBConnection->dbc->get_results($queryAraging,ARRAY_A);
        $araging = trim($result[0]["araging"]);
        $aragingArray = explode(" ", $araging);
        $aragingArraysize = count($aragingArray);

        //construct 'where clause' for filter AR
        $WHERE .= " AND (";

        for($i = 0; $i < $aragingArraysize; $i++){

            $WHERE .= " skada LIKE '".$aragingArray[$i]."%'";
            if($i == ($aragingArraysize - 1)){
                $WHERE .= ")";
                break;
            }
            $WHERE .= " OR";

        }
}else if($group=="NOTAP"){
        //get field apaging from tblprofile
        $queryApaging = "select apaging from tblprofile";
        $result  = $DBConnection->dbc->get_results($queryApaging,ARRAY_A);
        $apaging = trim($result[0]["apaging"]);
        $apagingArray = explode(" ", $apaging);
        $apagingArraysize = count($apagingArray);

        //construct 'where clause' for filter AP
        $WHERE .= " AND (";

        for($i = 0; $i < $apagingArraysize; $i++){

            $WHERE .= " skada LIKE '".$apagingArray[$i]."%'";
            if($i == ($apagingArraysize - 1)){
                $WHERE .= ")";
                break;
            }
            $WHERE .= " OR ";

        }
}else if($group=="NOTAR"){
        //get field araging from tblprofile
        $queryAraging = "select araging from tblprofile";
        $result  = $DBConnection->dbc->get_results($queryAraging,ARRAY_A);
        $araging = trim($result[0]["araging"]);
        $aragingArray = explode(" ", $araging);
        $aragingArraysize = count($aragingArray);

        //construct 'where clause' for filter AR
        $WHERE .= " AND (";

        for($i = 0; $i < $aragingArraysize; $i++){

            $WHERE .= " skada NOT LIKE '".$aragingArray[$i]."%'";
            if($i == ($aragingArraysize - 1)){
                $WHERE .= ")";
                break;
            }
            $WHERE .= " AND";

        }
}else if($group=="HUTANGAP"){
        //get field dm_hutang from tblprofile
        $queryApHutang = "select dm_hutang from tblprofile";
        $result  = $DBConnection->dbc->get_results($queryApHutang,ARRAY_A);
        $aphutang = trim($result[0]["dm_hutang"]);
        $aphutangArray = explode(" ", $aphutang);
        $aphutangArraysize = count($aphutangArray);

        //construct 'where clause' for filter AR
        $WHERE .= " AND (";

        for($i = 0; $i < $aphutangArraysize; $i++){

            $WHERE .= " skada LIKE '".$aphutangArray[$i]."%'";
            if($i == ($aphutangArraysize - 1)){
                $WHERE .= ")";
                break;
            }
            $WHERE .= " AND";

        }
}else if($group=="HUTANGBS"){
        //get field dm_hutang from tblprofile
        $queryBsHutang = "select bs from tblprofile";
        $result  = $DBConnection->dbc->get_results($queryBsHutang,ARRAY_A);
        $bshutang = trim($result[0]["bs"]);
        $bshutangArray = explode(" ", $bshutang);
        $bshutangArraysize = count($bshutangArray);

        //construct 'where clause' for filter AR
        $WHERE .= " AND (";

        for($i = 0; $i < $bshutangArraysize; $i++){

            $WHERE .= " skada LIKE '".$bshutangArray[$i]."%'";
            if($i == ($bshutangArraysize - 1)){
                $WHERE .= ")";
                break;
            }
            $WHERE .= " AND";

        }
}

//-------------------------------------------------------



$SQL = $SQL.$WHERE." ORDER BY SKADA";
//echo "$SQL"; exit;
$SQLCount = $SQLCount.$WHERE;

//******* SHOW FORM CONTENT *********
//$PageNum = 1;
//$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");
//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName;
    for ($x=1;$x<=$PageSize;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("tblcustomer",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;

}

?>
